/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.viettel.bkno.visionone.services;

import com.viettel.bkno.visionone.business.interfaces.IUserManager;
import com.viettel.bkno.visionone.entities.CachedInfo;
import com.viettel.bkno.visionone.entities.Role;
import com.viettel.bkno.visionone.entities.User;
import com.viettel.bkno.visionone.exception.CoreServiceException;
import com.viettel.bkno.visionone.exception.CoreServiceFault;
import com.viettel.bkno.visionone.exception.FaultType;
import javax.jws.WebService;
import javax.jws.WebMethod;
import javax.jws.WebParam;

/**
 *
 * @author hien
 */
@WebService(serviceName = "AdminServices")
public class AdminServices {
    static IUserManager userManager;
    
    static {
        //Start service manager
        ServicesManager.getInstance().start();
        
        userManager = ServicesManager.getInstance().getUserManager();
    }
    
    
    /**
     * Web service operation
     * @param username
     * @param password
     * @return 
     * @throws com.viettel.bkno.visionone.exception.CoreServiceException 
     */
    @WebMethod(operationName = "authen")
    public String getSessionToken(@WebParam(name = "username") String username, @WebParam(name = "password") String password) throws CoreServiceException {
        //TODO write your implementation code here:
        String token = userManager.authen(username, password);
        return token;
    }
    
    /**
     * Ham nay dung de set supper user ban dau, ko public
     * @param user 
     * @throws com.viettel.bkno.visionone.exception.CoreServiceException 
     */
    private void addPrivateUser(User user) throws CoreServiceException {
        userManager.addUser(user);
    }

    /**
     * Web service operation
     * @param username
     * @param token
     * @param user
     * @throws com.viettel.bkno.visionone.exception.CoreServiceException
     */
    @WebMethod(operationName = "addUser")
    public void addUser(@WebParam(name = "username") String username, @WebParam(name="token") String token,  @WebParam(name = "user") User user) throws CoreServiceException {
        //TODO write your implementation code here:
        CachedInfo sessionInfo = userManager.getSessionInfo(username, token);
        Role userRole = sessionInfo.getUser().getRole();
        switch (userRole) {
            case ADMIN:
                // add user truc tiep
                addPrivateUser(user);
                break;
            case SUB_ADMIN:
                // kiem tra user.getRole, neu role la admin hoac sub admin -> reject.
                if (user.getRole().equals(Role.NORMAL_USER)) {
                    addPrivateUser(user);
                } else {
                    CoreServiceFault fault = new CoreServiceFault();
                    fault.setFaultCode(FaultType.PERMISSION_DENIED.getCode());
                    fault.setFaultString(FaultType.PERMISSION_DENIED.getName());
                    throw new CoreServiceException(fault);
                }
                break;
            case NORMAL_USER:
                //Normal user khong co quyen tao user
                CoreServiceFault fault = new CoreServiceFault();
                fault.setFaultCode(FaultType.PERMISSION_DENIED.getCode());
                fault.setFaultString(FaultType.PERMISSION_DENIED.getName());
                throw new CoreServiceException(fault);
        }
    }

    @WebMethod(operationName = "updateUser")
    public void updateUser(@WebParam(name = "username") String username, @WebParam(name="token") String token, @WebParam(name="user") User user) throws CoreServiceException {
        //TODO viet service update user.
    }
    
    @WebMethod(operationName = "deleteUser")
    public void deleteUser(@WebParam(name = "username") String username, @WebParam(name="token") String token, @WebParam(name="delUsername") String delUsername) throws CoreServiceException {
        //TODO viet service delete user.
    }
}
